﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.IO;

namespace Scan
{
    public partial class frmLogin : Form
    {
        private OperationClass _operationClass;
        private delegate void UIDelegateMethod();

        public frmLogin(OperationClass operationClass)
        {
            InitializeComponent();
            _operationClass = operationClass;

        }

        private void UpdateForm()
        {
            if (this.InvokeRequired)
            {
                UIDelegateMethod ui = new UIDelegateMethod(UpdateForm);
                this.Invoke(ui, new object[] {});
            }
            else
            {
                this.Close();
            }
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MyJieGuo1(IAsyncResult iar)
        {
            try
            {
                userIno.User user = (userIno.User)iar.AsyncState;
                PublicVar.COMID = user.EndUserIsGsID(iar);
            }
            catch (Exception ex)
            {
                MessageBox.Show("异步获取公司ID异常" + ex.Message);
                return;
            }
        }

        private void MyJieGuo2(IAsyncResult iar)
        {
            try
            {
                userIno.User user = (userIno.User)iar.AsyncState;
                PublicVar.COMNAME = user.EndUserGsName(iar);
            }
            catch (Exception ex)
            {
                MessageBox.Show("异步获取公司名称异常" + ex.Message);
                return;
            }
        }

        private void MyJieGuo(IAsyncResult iar)
        {
            try
            {
                userIno.User user = (userIno.User)iar.AsyncState;
                bool strReturn = user.EndUserPassIsTrue(iar);

                if (strReturn == false)
                {
                    MessageBox.Show("用户名或密码错误！");
                    UpdateForm();
                    return;
                }

                PublicVar.IsLogin = true;

                while ((PublicVar.COMID == "") || (PublicVar.COMNAME == ""))
                {
                    System.Threading.Thread.Sleep(1000);
                }

                if ((_operationClass == OperationClass.cuku) || (_operationClass == OperationClass.ruku))
                {
                    frmWl rk = new frmWl(_operationClass);
                    UpdateForm();
                    rk.ShowDialog();
                }
                else
                {
                    frmXiang xiang = new frmXiang(_operationClass);
                    UpdateForm();
                    xiang.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("用户名密码异步验证时异常" + ex.Message);
                return;
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入用户名和密码！");
                this.Close();
                return;
            }

            userIno.User user = new userIno.User();
            //com.shnb12315.api.User user = new com.shnb12315.api.User();

            user.Url = PublicVar.USERURL;
            //user.GetUserInfo("");
            //user.Url = "http://114.55.143.172:808/User.asmx";
            try
            {
                //user.BeginUserPassIsTrue(textBox1.Text.Trim(), textBox2.Text.Trim(), new AsyncCallback(MyJieGuo、 ), user);
                //user.BeginUserIsGsID(textBox1.Text.Trim(), new AsyncCallback(MyJieGuo1), user);
                //user.BeginUserGsName(textBox1.Text.Trim(), new AsyncCallback(MyJieGuo2), user);
                //PublicVar.UserId = textBox1.Text;
                if (user.UserPassIsTrue(textBox1.Text.Trim(), textBox2.Text.Trim()))
                {
                    PublicVar.IsLogin = true;
                    PublicVar.COMNAME = user.UserGsName(textBox1.Text.Trim());

                    if (checkBox1.Checked)
                    {
                        edit_config.ConfigSetValue("UserName", textBox1.Text);
                        edit_config.ConfigSetValue("PassWord", textBox2.Text);
                        edit_config.ConfigSetValue("IsShowUserNameAndPassWord", "1");
                    }
                    else
                    {
                        edit_config.ConfigSetValue("IsShowUserNameAndPassWord", "0");
                    }

                    label3.Visible = true;
                    progressBar1.Visible = true;
                    timer1.Enabled = true;


                    DataSet ds = new DataSet();
                    ds = user.GetUserInfo(textBox1.Text.Trim());
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            PublicVar.UserJxsDj = Convert.ToInt32(ds.Tables[0].Rows[0]["用户等级"].ToString());
                            PublicVar.UserIngJxsCode = ds.Tables[0].Rows[0]["经销商代码"].ToString();
                            PublicVar.COMID = ds.Tables[0].Rows[0]["公司ID"].ToString();
                            PublicVar.UserId = textBox1.Text;
                        }

                        if (PublicVar.UserJxsDj > 0)
                        {
                            DataSet dsjxsmc = new DataSet();
                            Jxs.Jxs jxs = new global::Scan.Jxs.Jxs();
                            jxs.Url = PublicVar.JXSURL;
                            dsjxsmc = jxs.Select_Jxs_Info(Convert.ToInt32(PublicVar.UserIngJxsCode));
                            if (dsjxsmc.Tables.Count > 0)
                            {
                                if (dsjxsmc.Tables[0].Rows.Count > 0)
                                {
                                    PublicVar.UserIngJxsName = dsjxsmc.Tables[0].Rows[0]["经销商名称"].ToString();
                                }
                            }
                        }


                        if ((_operationClass == OperationClass.cuku) || (_operationClass == OperationClass.ruku) || (_operationClass == OperationClass.tuihuo))
                        {
                            frmWl rk = new frmWl(_operationClass);
                            UpdateForm();
                            rk.ShowDialog();
                        }
                        else
                        {
                            frmXiang xiang = new frmXiang(_operationClass);
                            UpdateForm();
                            xiang.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show("获取用户信息出错，请联系管理员！");
                        this.Close();
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！");
                    this.Close();
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("登录时异常" + ex.Message);
                return;
            }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(PublicVar.StrBanBen);
            sb.Append("\r\n");
            sb.Append(PublicVar.StrRiQi);
            MessageBox.Show(sb.ToString());
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            string a = edit_config.ConfigGetValue("UserName");
            string b = edit_config.ConfigGetValue("PassWord");
            string c= edit_config.ConfigGetValue("IsShowUserNameAndPassWord");

            if (c == "1")
            {
                textBox1.Text = a;
                textBox2.Text = b;
                checkBox1.Checked = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value >= 100)
            {
                progressBar1.Value = 0;
            }
            else
            {
                progressBar1.Value += 5;
            }
        }

        
        
    }
}